/**
 * 联系人数据模拟模块
 * 专门为Contact组件提供更丰富的模拟数据
 * @author LYC
 * @date 2025-09-17
 */

import Mock from 'mockjs'
import type { Contact, ContactCategory } from '@/store/contact/type'
import { currentUser } from './chatData'
import { Users, User, Circle, Star } from 'lucide-vue-next'

const Random = Mock.Random

// 生成随机联系人数据
export const generateContacts = (count: number = 30): Contact[] => {
  const contacts: Contact[] = []
  const statusOptions: Array<'online' | 'away' | 'busy' | 'offline'> = [
    'online',
    'away',
    'busy',
    'offline'
  ]

  // 添加固定联系人 - XL
  contacts.push({
    id: 'xl-user',
    name: 'XL',
    avatar: './src/assets/imgs/XL.png',
    status: 'online',
    lastMessage: '期待看到最终成果',
    type: 'friend'
  })

  // 添加固定联系人 - 工作群
  contacts.push({
    id: 'work-group',
    name: '研发部工作群',
    avatar: Random.image('100x100', '#4A90E2', '#FFF', 'png', '研发'),
    status: 'online',
    lastMessage: '[张总] 今天下午3点开会讨论项目进度',
    type: 'group'
  })

  // 添加固定联系人 - 家人群
  contacts.push({
    id: 'family-group',
    name: '家人群',
    avatar: Random.image('100x100', '#FF6B6B', '#FFF', 'png', '家'),
    status: 'online',
    lastMessage: '[妈妈] 周末回来吃饭吗？',
    type: 'group'
  })

  // 添加随机联系人
  for (let i = 1; i <= count; i++) {
    const firstName = Random.cfirst()
    const lastName = Random.clast()
    const name = lastName + firstName
    const isGroup = Random.boolean(0.2) // 20%概率是群组

    let avatar: string
    if (isGroup) {
      const groupThemes = ['项目', '兴趣', '同学', '朋友', '旅游', '游戏']
      const groupTheme = Random.pick(groupThemes)
      const groupColor = Random.pick([
        '#4A90E2',
        '#50E3C2',
        '#F5A623',
        '#D0021B',
        '#9013FE',
        '#417505'
      ])
      avatar = Random.image('100x100', groupColor, '#FFF', 'png', groupTheme.substring(0, 1))
    } else {
      avatar = Random.image('100x100', Random.color(), '#FFF', 'png', name.substring(0, 1))
    }

    contacts.push({
      id: `contact-${i}`,
      name: isGroup
        ? `${Random.cword(2, 4)}${Random.pick(['群', '小组', '俱乐部', '团队'])}`
        : name,
      avatar,
      status: Random.pick(statusOptions),
      lastMessage: isGroup
        ? `[${Random.cname()}] ${Random.csentence(5, 15)}`
        : Random.csentence(5, 15),
      type: isGroup ? 'group' : 'friend'
    })
  }

  return contacts
}

// 生成联系人分类数据
export const generateContactCategories = (): ContactCategory[] => {
  return [
    {
      type: 'all',
      name: '全部联系人',
      icon: Users,
      count: 0 // 将在使用时动态计算
    },
    {
      type: 'friend',
      name: '好友',
      icon: User,
      count: 0
    },
    {
      type: 'group',
      name: '群组',
      icon: Users,
      count: 0
    },
    {
      type: 'online',
      name: '在线',
      icon: Circle,
      count: 0
    },
    {
      type: 'favorite',
      name: '收藏',
      icon: Star,
      count: 0
    }
  ]
}

// 生成带有详细信息的联系人数据
export const generateDetailedContacts = (count: number = 30): Contact[] => {
  const contacts = generateContacts(count)

  return contacts.map((contact) => {
    // 为每个联系人添加更多详细信息
    const isGroup = contact.type === 'group'

    return {
      ...contact,
      email: isGroup
        ? undefined
        : `${Random.word(5, 10)}@${Random.pick(['gmail.com', 'outlook.com', 'qq.com', '163.com'])}`,
      phone: isGroup
        ? undefined
        : `1${Random.pick(['3', '5', '7', '8', '9'])}${Random.string('0123456789', 9)}`,
      location: isGroup ? undefined : `${Random.province()}-${Random.city()}`,
      bio: isGroup ? `${Random.csentence(10, 30)}` : `${Random.csentence(10, 20)}`,
      tags: Random.shuffle(['工作', '朋友', '家人', '同学', '重要', '项目']).slice(
        0,
        Random.integer(0, 3)
      ),
      isFavorite: Random.boolean(0.2), // 20%概率是收藏的联系人
      createdAt: new Date(Date.now() - Random.integer(1, 1000) * 86400000),
      lastContact: new Date(Date.now() - Random.integer(0, 30) * 86400000),
      memberCount: isGroup ? Random.integer(3, 100) : undefined,
      owner: isGroup
        ? Random.boolean(0.7)
          ? currentUser.id
          : `user-${Random.integer(1, 10)}`
        : undefined,
      // 群组特有属性
      members: isGroup
        ? Array(Random.integer(3, 10))
            .fill(0)
            .map((_, idx) => ({
              id: `member-${contact.id}-${idx}`,
              name: Random.cname(),
              avatar: Random.image('100x100', Random.color(), '#FFF', 'png', Random.cfirst()),
              role: Random.pick(['owner', 'admin', 'member']),
              joinTime: new Date(Date.now() - Random.integer(1, 365) * 86400000)
            }))
        : undefined,
      announcement: isGroup ? (Random.boolean(0.7) ? Random.csentence(20, 100) : '') : undefined
    }
  })
}

// 生成完整的联系人数据
export const generateContactData = (): { contacts: Contact[]; categories: ContactCategory[] } => {
  const contacts = generateDetailedContacts(30)
  const categories = generateContactCategories()

  // 更新分类计数
  categories.forEach((category) => {
    switch (category.type) {
      case 'all':
        category.count = contacts.length
        break
      case 'friend':
        category.count = contacts.filter((c) => c.type === 'friend').length
        break
      case 'group':
        category.count = contacts.filter((c) => c.type === 'group').length
        break
      case 'online':
        category.count = contacts.filter((c) => c.status === 'online').length
        break
      case 'favorite':
        // 使用类型断言来访问扩展属性
        category.count = contacts.filter(
          (c) => (c as { isFavorite?: boolean }).isFavorite === true
        ).length
        break
    }
  })

  return {
    contacts,
    categories
  }
}

export default {
  generateContacts,
  generateContactCategories,
  generateDetailedContacts,
  generateContactData
}
